﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.Business.Proj;
using ESOL.Business.Sales;
using ESOL.Business.Mst;
using ESOL.Business.Prod;
using ESOL.Business.Bpt;
using ESOL.Business.Inv;
using ESOL.Business.Pur;
using ESOL.WinUI.Pur;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainProductPlanOrderCreate : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        private List<ProdPlanOrderEntity> listSelectedPlanOrder=new List<ProdPlanOrderEntity>();
        private PurApplicationEntity mSelectedPurApplication;
        #endregion

        #region ctor
        public frmMaintainProductPlanOrderCreate(ProdPlanOrderEntity m)
        {
            InitializeComponent();
            listSelectedPlanOrder.Add(m);
        }
        public frmMaintainProductPlanOrderCreate(List<ProdPlanOrderEntity> list)
        {
            InitializeComponent();
            this.listSelectedPlanOrder = list;
        }
        #endregion

        #region functions
        private void SetField()
        {
            cmbStatus.SetSelectedValue("STATUS01");
        }

        private void BindCombobox()
        {
            string sCondition = " and A.LOCKED=0 and A.DELETE_FLAG=0";
            DataTable dtWarehouse = MstWarehouseBs.GetByCondition(sCondition, "", "");
            DataRow drWarehouse = dtWarehouse.NewRow();
            dtWarehouse.Rows.InsertAt(drWarehouse, 0);
            cmbWarehouse.DisplayMember = "WAREHOUSE_NAME";
            cmbWarehouse.ValueMember = "WAREHOUSE_CODE";
            cmbWarehouse.DataSource = dtWarehouse;

            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbApplicationType, "PURTYPE", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colChangedItemStatus, "POI", false);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colOperationType, typeof(WorkingOrderOperationType), typeof(int), false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colWorkingOrderStatus, "POI", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "PURST", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit2, "STATUS", true);

            DataTable dtIsMark = new DataTable();
            dtIsMark.Columns.Add("name");
            dtIsMark.Columns.Add("value");
            dtIsMark.Rows.Add("是", 1);
            dtIsMark.Rows.Add("否", 0);
            colIsRemark.ValueMember = "value";
            colIsRemark.DisplayMember = "name";
            colIsRemark.DataSource = dtIsMark;
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(cmbWarehouse.SelectedValue.ToString()))
            {
                MessageBox.Show("仓库不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            DataTable dtPur = gvPurchaseOrder.DataSource as DataTable;
            DataTable dtWorking = gvWorkingOrder.DataSource as DataTable;
            if (dtWorking.Rows.Count > 0)
            {
                foreach (DataRow dr in dtWorking.Rows)
                {
                    if (Convert.ToDecimal(dr["ACTUAL_STORAGE_QTY"].ToString()) - Convert.ToDecimal(dr["MOVE_QTY"].ToString()) < 0)
                    {
                        MessageBox.Show("移库数量大于原库存量！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
            }
            if (dtPur.Rows.Count > 0)
            {
                if (string.IsNullOrEmpty(cmbApplicationType.SelectedValue.ToString()))
                {
                    MessageBox.Show("采购单类型不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    xtraTabPage2.Select();
                    cmbApplicationType.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()))
                {
                    MessageBox.Show("采购单类型不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    xtraTabPage2.Select();
                    cmbStatus.Focus();
                    return false;
                }
                if (dtpRequiredDate.EditValue == null)
                {
                    MessageBox.Show("要求交期不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    xtraTabPage2.Select();
                    dtpRequiredDate.Focus();
                    return false;
                }
            }
            else if (dtWorking.Rows.Count == 0 && dtWorking.Rows.Count == 0)
            {
                MessageBox.Show("生单数据不存在，无法生单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private void SetEntity()
        {
            DataTable dtPur = gvPurchaseOrder.DataSource as DataTable;
            if (dtPur.Rows.Count > 0)
            {
                if (cbIsProduct.Checked)
                {
                    mSelectedPurApplication.IsProductPurchase = 0;
                }
                else
                {
                    mSelectedPurApplication.IsProductPurchase = 1;
                }
                mSelectedPurApplication.PlanOrderId = listSelectedPlanOrder[0].PlanOrderId;
                mSelectedPurApplication.PurchaseType = cmbApplicationType.SelectedValue.ToString();
                mSelectedPurApplication.ApplicationPerson = Common.CommonParameter.CurrentUser.PersonId;
                mSelectedPurApplication.ApplicationDate = DateTime.Now;
                mSelectedPurApplication.ApprovedDate = DateTime.Now;
                mSelectedPurApplication.DeliveryRequiredDate = (DateTime)dtpRequiredDate.EditValue;
                mSelectedPurApplication.Status = cmbStatus.SelectedValue.ToString();
                mSelectedPurApplication.Remark = txtRemark.Text;
                mSelectedPurApplication.CreatedBy = Common.CommonParameter.CurrentUser.PersonId;
                mSelectedPurApplication.CreatedDate = DateTime.Now;
                mSelectedPurApplication.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;
                mSelectedPurApplication.LastModifiedDate = DateTime.Now;
            }
        }

        private void LoadWorkingOrderData()
        {
            DataTable dtWorkingOrder = new DataTable();
            DataTable dtChangedOrderItem = new DataTable();
            dtWorkingOrder.Columns.Add("PLAN_ORDER_ITEM_ID");
            dtWorkingOrder.Columns.Add("PLAN_ORDER_ID");
            dtWorkingOrder.Columns.Add("BOM_ID");
            dtWorkingOrder.Columns.Add("RELATED_PLAN_ORDER_ITEM_ID");
            dtWorkingOrder.Columns.Add("PRODUCT_ID");
            dtWorkingOrder.Columns.Add("BLUEPRINT_ID");
            dtWorkingOrder.Columns.Add("TECHNIC_PERSON");
            dtWorkingOrder.Columns.Add("ORDER_ITEM_CODE");
            dtWorkingOrder.Columns.Add("DELIVERY_DATE", typeof(DateTime));

            dtWorkingOrder.Columns.Add("STORAGE_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("PRODUCTED_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("PURCHASE_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("STATUS");
            dtWorkingOrder.Columns.Add("REMARK");
            dtWorkingOrder.Columns.Add("PRODUCT_TYPE");
            dtWorkingOrder.Columns.Add("WORKORDER_CREATE_DATE");
            dtWorkingOrder.Columns.Add("MATERIAL_TOTAL_WEIGHT");
            dtWorkingOrder.Columns.Add("MATERIAL_TOTAL_QTY");
            dtWorkingOrder.Columns.Add("ORDER_TYPE");
            dtWorkingOrder.Columns.Add("IS_ASSEMBLY");
            dtWorkingOrder.Columns.Add("IS_PRINTED");
            dtWorkingOrder.Columns.Add("IS_MARK");
            dtWorkingOrder.Columns.Add("PLAN_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("ACTUAL_STORAGE_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("MOVE_QTY", typeof(decimal));
            dtWorkingOrder.Columns.Add("AVAILABLE_QTY",typeof(decimal));

            dtWorkingOrder.Columns.Add("PRODUCT_PLAN_ORDER");
            dtWorkingOrder.Columns.Add("PRODUCT_NAME");
            dtWorkingOrder.Columns.Add("DESCRIPTION");

            foreach (ProdPlanOrderEntity m in listSelectedPlanOrder)
            {
                SalesProdPlanApplicationItemEntity mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(m.ApplicationItemId);
                if (mApplicationItem == null)
                    continue;
                MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mApplicationItem.ProductId);
                if (mProduct == null)
                    continue;
                //DataTable dt = BomProductBomMasterBs.GetDtProductBomByProductID(mProduct.ProductId, "PDTYPE01", "PDTYPE01");
                DataTable dt = ProjMBOMBs.GetDtProductBomByBomID(m.BomId, "PDTYPE01", "PDTYPE01");
                if (dt.Rows.Count == 0)
                    continue;
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["IS_CUSTOMER_FOR"].ToString().Equals("1"))    //客户提供
                        continue;
                    DataRow drWorkingOrder = dtWorkingOrder.NewRow();
                    drWorkingOrder["PLAN_ORDER_ITEM_ID"] = Guid.NewGuid().ToString();
                    drWorkingOrder["PLAN_ORDER_ID"] = m.PlanOrderId;
                    drWorkingOrder["BOM_ID"] = dr["BOM_ID"].ToString();
                    drWorkingOrder["PRODUCT_ID"] = dr["PRODUCT_ID"].ToString();
                    BptBlueprintEntity mBlueprint = BptBlueprintBs.GetBptBlueprintByProductID(dr["PRODUCT_ID"].ToString());
                    if(mBlueprint!=null)
                    drWorkingOrder["BLUEPRINT_ID"] = mBlueprint.BlueprintId;
                    drWorkingOrder["TECHNIC_PERSON"] = Common.CommonParameter.CurrentUser.PersonId;
                    drWorkingOrder["DELIVERY_DATE"] = m.DeliveryDate;
                    decimal dPlanQty = m.Qty * Convert.ToDecimal(dr["UNIT_DOSE"].ToString());
                    drWorkingOrder["PLAN_QTY"] = dPlanQty;
                    drWorkingOrder["PRODUCTED_QTY"] = 0;
                    drWorkingOrder["STATUS"] = "POI01";
                    drWorkingOrder["PRODUCT_TYPE"] = "PDTYPE01";
                    drWorkingOrder["ORDER_TYPE"] = "POIT01";

                    drWorkingOrder["PRODUCT_PLAN_ORDER"] = m.PlanOrderCode;
                    drWorkingOrder["PRODUCT_NAME"] = dr["PRODUCT_NAME"].ToString();
                    drWorkingOrder["DESCRIPTION"] = dr["DESCRIPTION"].ToString();
                    drWorkingOrder["IS_MARK"] = m.IsMark;
                    decimal dActualStorageQty = InvStorageBs.GetActualStorageQty(dr["PRODUCT_ID"].ToString());
                    drWorkingOrder["ACTUAL_STORAGE_QTY"] = dActualStorageQty;
                    decimal dProductionConsumption=ProdUsedAmountTempBs.GetProductionConsumptionforID(dr["PRODUCT_ID"].ToString());
                    if (dActualStorageQty - dProductionConsumption >= 0)
                    {
                        drWorkingOrder["AVAILABLE_QTY"] = dActualStorageQty - dProductionConsumption;
                    }
                    else
                    {
                        drWorkingOrder["AVAILABLE_QTY"] = 0;
                    }
                    if (dActualStorageQty - dPlanQty >= 0)
                    {
                        drWorkingOrder["QTY"] = 0;
                        drWorkingOrder["MOVE_QTY"] = dPlanQty;
                    }
                    else
                    {
                        drWorkingOrder["QTY"] = -(dActualStorageQty - dPlanQty);
                        drWorkingOrder["MOVE_QTY"] = dActualStorageQty;
                    }
                    drWorkingOrder["WORKORDER_CREATE_DATE"] = DateTime.Now;
                    dtWorkingOrder.Rows.Add(drWorkingOrder);

                }
                if (m.OrderStatus == 0)
                {
                    continue;
                }
                dtChangedOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByPlanOrderID(m.ChangedOrderId);
                if (dtChangedOrderItem.Rows.Count == 0)
                {
                    continue;
                }
            }
            gvWorkingOrder.DataSource = dtWorkingOrder;
            if (dtChangedOrderItem.Rows.Count == 0)
            {
                return;
            }
            DataColumn dc = new DataColumn("OPERATION_TYPE", typeof(int));
            dc.DefaultValue = 1;
            dtChangedOrderItem.Columns.Add(dc);
            gvChangedOrderItem.DataSource = dtChangedOrderItem;
        }

        private void LoadPurchaseApplicationOrderData()
        {
            mSelectedPurApplication = new PurApplicationEntity();
            mSelectedPurApplication.ApplicationId = Guid.NewGuid().ToString();
            DataTable dtPurchaseOrderItem = new DataTable();
            dtPurchaseOrderItem.Columns.Add("APPLICATION_ITEM_ID");
            dtPurchaseOrderItem.Columns.Add("APPLICATION_ID");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_ID");
            dtPurchaseOrderItem.Columns.Add("PRICE", typeof(decimal));
            dtPurchaseOrderItem.Columns.Add("DELIVERY_REQUIRED_DATE", typeof(DateTime));
            dtPurchaseOrderItem.Columns.Add("DELIVERY_PLAN_DATE", typeof(DateTime));
            dtPurchaseOrderItem.Columns.Add("REMARK");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_SPEC_IN");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_SPEC_OUT");
            dtPurchaseOrderItem.Columns.Add("PLAN_ORDER_ITEM_ID");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_TECHNIC_ID");
            dtPurchaseOrderItem.Columns.Add("PLAN_QTY", typeof(decimal));
            dtPurchaseOrderItem.Columns.Add("ACTUAL_STORAGE_QTY", typeof(decimal));//实际库存量
            dtPurchaseOrderItem.Columns.Add("AVAILABLE_QTY", typeof(decimal)); //可用量
            dtPurchaseOrderItem.Columns.Add("QTY", typeof(decimal));//计划采购量
            dtPurchaseOrderItem.Columns.Add("MOVE_QTY", typeof(decimal));//移库量
            dtPurchaseOrderItem.Columns.Add("PRODUCT_CODE");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_NAME");
            dtPurchaseOrderItem.Columns.Add("DESCRIPTION");
            dtPurchaseOrderItem.Columns.Add("BOM_ID");

            foreach (ProdPlanOrderEntity m in listSelectedPlanOrder)
            {
                SalesProdPlanApplicationItemEntity mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(m.ApplicationItemId);
                if (mApplicationItem == null)
                    continue;
                MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mApplicationItem.ProductId);
                if (mProduct == null)
                    continue;
                DataTable dt = ProjMBOMBs.GetDtProductBomByBomID(m.BomId, "PDTYPE01", "PDTYPE02");
                if (dt.Rows.Count == 0)
                    continue;
                dt.Rows.RemoveAt(0);

                foreach (DataRow dr in dt.Rows)
                {
                    DataRow drPurchaseOrderItem = dtPurchaseOrderItem.NewRow();
                    drPurchaseOrderItem["APPLICATION_ITEM_ID"] = Guid.NewGuid().ToString();
                    drPurchaseOrderItem["APPLICATION_ID"] = mSelectedPurApplication.ApplicationId;
                    drPurchaseOrderItem["PRODUCT_ID"] = dr["PRODUCT_ID"].ToString();
                    drPurchaseOrderItem["PRICE"] = 0;
                    drPurchaseOrderItem["DELIVERY_REQUIRED_DATE"] = m.DeliveryDate;
                    drPurchaseOrderItem["REMARK"] = "由系统自动生单。";
                    decimal dPlanQty = m.Qty * Convert.ToDecimal(dr["UNIT_DOSE"].ToString());
                    drPurchaseOrderItem["PLAN_QTY"] = dPlanQty;
                    decimal dActualStorageQty = InvStorageBs.GetActualStorageQty(dr["PRODUCT_ID"].ToString());
                    drPurchaseOrderItem["ACTUAL_STORAGE_QTY"] = dActualStorageQty;
                    //可用量
                    decimal dProductionConsumption = ProdUsedAmountTempBs.GetProductionConsumptionforID(dr["PRODUCT_ID"].ToString());
                    if (dActualStorageQty - dProductionConsumption >= 0)
                    {
                        drPurchaseOrderItem["AVAILABLE_QTY"] = dActualStorageQty - dProductionConsumption;
                    }
                    else
                    {
                        drPurchaseOrderItem["AVAILABLE_QTY"] = 0;
                    }
                    if (dActualStorageQty - dPlanQty >= 0)
                    {
                        drPurchaseOrderItem["QTY"] = 0;
                        drPurchaseOrderItem["MOVE_QTY"] = dPlanQty;
                    }
                    else
                    {
                        drPurchaseOrderItem["QTY"] = -(dActualStorageQty - dPlanQty);
                        drPurchaseOrderItem["MOVE_QTY"] = dActualStorageQty;
                    }

                    drPurchaseOrderItem["PRODUCT_CODE"] = dr["PRODUCT_CODE"].ToString();
                    drPurchaseOrderItem["PRODUCT_NAME"] = dr["PRODUCT_NAME"].ToString();
                    drPurchaseOrderItem["DESCRIPTION"] = dr["DESCRIPTION"].ToString();
                    drPurchaseOrderItem["BOM_ID"] = dr["BOM_ID"].ToString();
                    dtPurchaseOrderItem.Rows.Add(drPurchaseOrderItem);

                }
            }
            gvPurchaseOrder.DataSource = dtPurchaseOrderItem;
        }

        private void LoadFinishedProductPurchaseApplicationOrderData()
        {
            mSelectedPurApplication = new PurApplicationEntity();
            mSelectedPurApplication.ApplicationId = Guid.NewGuid().ToString();
            DataTable dtPurchaseOrderItem = new DataTable();
            dtPurchaseOrderItem.Columns.Add("APPLICATION_ITEM_ID");
            dtPurchaseOrderItem.Columns.Add("APPLICATION_ID");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_ID");
            dtPurchaseOrderItem.Columns.Add("PRICE", typeof(decimal));
            dtPurchaseOrderItem.Columns.Add("DELIVERY_REQUIRED_DATE", typeof(DateTime));
            dtPurchaseOrderItem.Columns.Add("DELIVERY_PLAN_DATE", typeof(DateTime));
            dtPurchaseOrderItem.Columns.Add("REMARK");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_SPEC_IN");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_SPEC_OUT");
            dtPurchaseOrderItem.Columns.Add("PLAN_ORDER_ITEM_ID");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_TECHNIC_ID");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_PLAN_ORDER");
            dtPurchaseOrderItem.Columns.Add("PRODUCT_NAME");
            dtPurchaseOrderItem.Columns.Add("DESCRIPTION");
            dtPurchaseOrderItem.Columns.Add("PLAN_QTY", typeof(decimal));
            dtPurchaseOrderItem.Columns.Add("ACTUAL_STORAGE_QTY", typeof(decimal));
            dtPurchaseOrderItem.Columns.Add("QTY", typeof(decimal));
            dtPurchaseOrderItem.Columns.Add("MOVE_QTY", typeof(decimal));
            foreach (ProdPlanOrderEntity m in listSelectedPlanOrder)
            {
                SalesProdPlanApplicationItemEntity mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(m.ApplicationItemId);
                if (mApplicationItem == null)
                    continue;
                MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mApplicationItem.ProductId);
                if (mProduct == null)
                    continue;
                DataRow drPurchaseOrderItem = dtPurchaseOrderItem.NewRow();
                drPurchaseOrderItem["APPLICATION_ITEM_ID"] = Guid.NewGuid().ToString();
                drPurchaseOrderItem["APPLICATION_ID"] = mSelectedPurApplication.ApplicationId;
                drPurchaseOrderItem["PRODUCT_ID"] = mProduct.ProductId;
                drPurchaseOrderItem["PRICE"] = 0;
                drPurchaseOrderItem["DELIVERY_REQUIRED_DATE"] = m.DeliveryDate;
                drPurchaseOrderItem["REMARK"] = "由系统自动生单。";
                drPurchaseOrderItem["PRODUCT_PLAN_ORDER"] = m.PlanOrderCode;
                drPurchaseOrderItem["PRODUCT_NAME"] = mProduct.ProductName;
                drPurchaseOrderItem["DESCRIPTION"] = mProduct.Description;
                drPurchaseOrderItem["PLAN_QTY"] = m.Qty;
                drPurchaseOrderItem["QTY"] = m.Qty;
                drPurchaseOrderItem["MOVE_QTY"] = 0;
                dtPurchaseOrderItem.Rows.Add(drPurchaseOrderItem);
            }
            gvPurchaseOrder.DataSource = dtPurchaseOrderItem;
        }

        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            SetField();
            if (listSelectedPlanOrder[0].Status.Equals("PSTATUS01"))
            {
                LoadWorkingOrderData();
                LoadPurchaseApplicationOrderData();
            }
            else if (listSelectedPlanOrder[0].Status.Equals("PSTATUS02"))
            {
                LoadFinishedProductPurchaseApplicationOrderData();
            }
        }

        private void dtpRequiredDate_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dtpRequiredDate.EditValue.ToString()) == false)
            {
                DataTable dt = (DataTable)gvPurchaseOrder.DataSource;
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["DELIVERY_REQUIRED_DATE"] = (DateTime)dtpRequiredDate.EditValue;
                    }
                }
            }
        }

        protected override void Click_Save()
        {
            if (CheckData() == false)
                return;
            SetEntity();
            DataTable dtBom = ProjMBOMBs.GetDtProductBomByBomID(listSelectedPlanOrder[0].BomId, BomApproveStatus.已审核.ToString(), "");
            DataTable dtWorkingOrder = (DataTable)gvWorkingOrder.DataSource;
            DataTable dtPurchaseApplication = (DataTable)gvPurchaseOrder.DataSource;
            string sErrMsg = "";
            if (ProdPlanOrderItemBs.ProductPlanMakeOrder(cmbWarehouse.SelectedValue.ToString(),dtBom,
                listSelectedPlanOrder, dtWorkingOrder,
                mSelectedPurApplication, dtPurchaseApplication,
                gvChangedOrderItem.DataSource as DataTable, out sErrMsg) == true)
            {
                MessageBox.Show("生单成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                return;
            }
            else
            {
                MessageBox.Show("生单失败！" + sErrMsg, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        #endregion

        private void BindPOHis(string sProductCode)
        {
            if (string.IsNullOrEmpty(sProductCode))
                return;
            string orderBy = "PO.PURCHASE_ORDER_CODE DESC";
            string sCondition1 = " AND PO.STATUS IN ('PURST01','PURST02','PURST03','PURST035')";
            string sCondition2 = " AND C.PRODUCT_CODE='" + sProductCode + "'";
            DataTable dt = PurPurchaseOrderBs.GetByCondition(sCondition1, "", orderBy, "", sCondition2);
            gvPOHis.DataSource = dt;
        }
        private void BindPORHis(string sProductCode)
        {
             if (string.IsNullOrEmpty(sProductCode))
                return;
             string condition = " AND MP.PRODUCT_CODE='"+sProductCode+"'";
             condition += " AND PA.STATUS IN ('STATUS01','STATUS02','STATUS03','STATUS04')";
             string orderBy = "PA.APPLICATION_CODE DESC";
            DataTable dt=PurApplicationBs.GetByCondition(condition, orderBy, "");
            gvPORHis.DataSource = dt;
        }
        private void gridView2_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow dr = gridView2.GetFocusedDataRow();
            if (dr == null)
                return;
            if (string.IsNullOrEmpty(dr["PRODUCT_CODE"].ToString()) == true)
                return;
            //查询采购订单绑定数据
            BindPOHis(dr["PRODUCT_CODE"].ToString());
            //查询采购申请单绑定数据
            BindPORHis(dr["PRODUCT_CODE"].ToString());
        }

        private void tsmPODetail_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            PurPurchaseOrderEntity mPO=PurPurchaseOrderBs.GetPurchaseOrderByCode(dr["PURCHASE_ORDER_CODE"].ToString());
            frmMaintainPurchaseOrder frm = new frmMaintainPurchaseOrder();
            frm.ThisActionType = ActionType.View;
            frm.SelectedPurchaseOrder = mPO;
            frm.ShowDialog();
        }

        private void tsmPORDetail_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView8.GetFocusedDataRow();
            if (dr == null)
                return;
            PurApplicationEntity mPO = PurApplicationBs.GetPurApplicationByCode(dr["APPLICATION_CODE"].ToString());
            frmMaintainPurchaseApplication frm = new frmMaintainPurchaseApplication();
            frm.ThisActionType = ActionType.View;
            frm.SelectedPurApplication = mPO;
            frm.ShowDialog();
        }

    }
}
